/*==============================================================*/
/* DBMS name:      Sybase AS Enterprise 15.0.2                  */
/* Created on:     09/10/2012 02:51:41 p.m.                     */
/*==============================================================*/


if exists (select 1
            from  sysobjects
            where id = object_id('TR_BODEGA')
            and   type = 'TR')
   drop trigger TR_BODEGA
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_CARGO')
            and   type = 'TR')
   drop trigger TR_CARGO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_CLIENTE')
            and   type = 'TR')
   drop trigger TR_CLIENTE
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_CONTACTO')
            and   type = 'TR')
   drop trigger TR_CONTACTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_COTIZACION')
            and   type = 'TR')
   drop trigger TR_COTIZACION
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_DIVISION')
            and   type = 'TR')
   drop trigger TR_DIVISION
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_DOC_IMP')
            and   type = 'TR')
   drop trigger TR_DOC_IMP
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_EMPLEADO')
            and   type = 'TR')
   drop trigger TR_EMPLEADO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_EQUIPO')
            and   type = 'TR')
   drop trigger TR_EQUIPO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_FACTURA')
            and   type = 'TR')
   drop trigger TR_FACTURA
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_INCOTERM')
            and   type = 'TR')
   drop trigger TR_INCOTERM
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_LOCALIZACION')
            and   type = 'TR')
   drop trigger TR_LOCALIZACION
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_LOG_IN')
            and   type = 'TR')
   drop trigger TR_LOG_IN
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_MARCA')
            and   type = 'TR')
   drop trigger TR_MARCA
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_MODELO')
            and   type = 'TR')
   drop trigger TR_MODELO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_ORDEN_C')
            and   type = 'TR')
   drop trigger TR_ORDEN_C
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_PDV')
            and   type = 'TR')
   drop trigger TR_PDV
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_PRECIO')
            and   type = 'TR')
   drop trigger TR_PRECIO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_PRESUPUESTO')
            and   type = 'TR')
   drop trigger TR_PRESUPUESTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_PROVEEDOR')
            and   type = 'TR')
   drop trigger TR_PROVEEDOR
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_REGISTRO_ES')
            and   type = 'TR')
   drop trigger TR_REGISTRO_ES
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_REPUESTO')
            and   type = 'TR')
   drop trigger TR_REPUESTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_RESOLUCION')
            and   type = 'TR')
   drop trigger TR_RESOLUCION
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_SOLICITUD_B')
            and   type = 'TR')
   drop trigger TR_SOLICITUD_B
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_TIPO_DE_EQUIPO')
            and   type = 'TR')
   drop trigger TR_TIPO_DE_EQUIPO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_TIPO_DE_REPUESTO')
            and   type = 'TR')
   drop trigger TR_TIPO_DE_REPUESTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TR_TIPO_DOC_IMP')
            and   type = 'TR')
   drop trigger TR_TIPO_DOC_IMP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_BITACORA')
            and   type = 'V')
   drop view V_BITACORA
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_BODEGA')
            and   type = 'V')
   drop view V_BODEGA
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_CARGO')
            and   type = 'V')
   drop view V_CARGO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_CLIENTE')
            and   type = 'V')
   drop view V_CLIENTE
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_CONTACTO')
            and   type = 'V')
   drop view V_CONTACTO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_COTIZACION')
            and   type = 'V')
   drop view V_COTIZACION
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_COT_EQU')
            and   type = 'V')
   drop view V_COT_EQU
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_COT_REP')
            and   type = 'V')
   drop view V_COT_REP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_DIVISION')
            and   type = 'V')
   drop view V_DIVISION
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_DOC_IMP')
            and   type = 'V')
   drop view V_DOC_IMP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_EMPLEADO')
            and   type = 'V')
   drop view V_EMPLEADO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_EQUIPO')
            and   type = 'V')
   drop view V_EQUIPO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_EQU_BOD')
            and   type = 'V')
   drop view V_EQU_BOD
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_EQU_PDV')
            and   type = 'V')
   drop view V_EQU_PDV
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_FACTURA')
            and   type = 'V')
   drop view V_FACTURA
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_FAC_EQU')
            and   type = 'V')
   drop view V_FAC_EQU
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_FAC_REP')
            and   type = 'V')
   drop view V_FAC_REP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_INCOTERM')
            and   type = 'V')
   drop view V_INCOTERM
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_LOCALIZACION')
            and   type = 'V')
   drop view V_LOCALIZACION
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_LOGIN')
            and   type = 'V')
   drop view V_LOGIN
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_MARCA')
            and   type = 'V')
   drop view V_MARCA
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_MODELO')
            and   type = 'V')
   drop view V_MODELO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_OC_EQU')
            and   type = 'V')
   drop view V_OC_EQU
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_OC_REP')
            and   type = 'V')
   drop view V_OC_REP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_ORDEN_C')
            and   type = 'V')
   drop view V_ORDEN_C
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_PDV')
            and   type = 'V')
   drop view V_PDV
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_PRECIO')
            and   type = 'V')
   drop view V_PRECIO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_PRESUPUESTO')
            and   type = 'V')
   drop view V_PRESUPUESTO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_PROVEEDOR')
            and   type = 'V')
   drop view V_PROVEEDOR
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_PROV_REP')
            and   type = 'V')
   drop view V_PROV_REP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REGISTRO_ES')
            and   type = 'V')
   drop view V_REGISTRO_ES
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REG_EQU')
            and   type = 'V')
   drop view V_REG_EQU
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REG_REP')
            and   type = 'V')
   drop view V_REG_REP
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REPUESTO')
            and   type = 'V')
   drop view V_REPUESTO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REP_BOD')
            and   type = 'V')
   drop view V_REP_BOD
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_REP_PDV')
            and   type = 'V')
   drop view V_REP_PDV
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_RESOLUCION')
            and   type = 'V')
   drop view V_RESOLUCION
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_SOLICITUD_B')
            and   type = 'V')
   drop view V_SOLICITUD_B
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_TIPO_DE_EQUIPO')
            and   type = 'V')
   drop view V_TIPO_DE_EQUIPO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_TIPO_DE_REPUESTO')
            and   type = 'V')
   drop view V_TIPO_DE_REPUESTO
go

if exists (select 1
            from  sysobjects
            where  id = object_id('V_TIPO_DOC_IMP')
            and   type = 'V')
   drop view V_TIPO_DOC_IMP
go

if exists (select 1
            from  sysobjects
            where id = object_id('BITACORA')
            and   type = 'U')
   drop table BITACORA
go

if exists (select 1
            from  sysobjects
            where id = object_id('BODEGA')
            and   type = 'U')
   drop table BODEGA
go

if exists (select 1
            from  sysobjects
            where id = object_id('CARGO')
            and   type = 'U')
   drop table CARGO
go

if exists (select 1
            from  sysobjects
            where id = object_id('CLIENTE')
            and   type = 'U')
   drop table CLIENTE
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('CONTACTO')
            and   name  = 'CLI_CONT_FK'
            and   indid > 0
            and   indid < 255)
   drop index CONTACTO.CLI_CONT_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('CONTACTO')
            and   type = 'U')
   drop table CONTACTO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('COTIZACION')
            and   name  = 'COT_CLI_FK'
            and   indid > 0
            and   indid < 255)
   drop index COTIZACION.COT_CLI_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('COTIZACION')
            and   type = 'U')
   drop table COTIZACION
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('COT_EQU')
            and   name  = 'CONTIENE2_FK'
            and   indid > 0
            and   indid < 255)
   drop index COT_EQU.CONTIENE2_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('COT_EQU')
            and   name  = 'ASIGNA2_FK'
            and   indid > 0
            and   indid < 255)
   drop index COT_EQU.ASIGNA2_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('COT_EQU')
            and   type = 'U')
   drop table COT_EQU
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('COT_REP')
            and   name  = 'CONTIENE_FK'
            and   indid > 0
            and   indid < 255)
   drop index COT_REP.CONTIENE_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('COT_REP')
            and   name  = 'ASIGNA_FK'
            and   indid > 0
            and   indid < 255)
   drop index COT_REP.ASIGNA_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('COT_REP')
            and   type = 'U')
   drop table COT_REP
go

if exists (select 1
            from  sysobjects
            where id = object_id('DIVISION')
            and   type = 'U')
   drop table DIVISION
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('DOC_IMP')
            and   name  = 'OC_DOCI_FK'
            and   indid > 0
            and   indid < 255)
   drop index DOC_IMP.OC_DOCI_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('DOC_IMP')
            and   name  = 'TDOCI_DOCI_FK'
            and   indid > 0
            and   indid < 255)
   drop index DOC_IMP.TDOCI_DOCI_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('DOC_IMP')
            and   type = 'U')
   drop table DOC_IMP
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EMPLEADO')
            and   name  = 'EMP_CAR_FK'
            and   indid > 0
            and   indid < 255)
   drop index EMPLEADO.EMP_CAR_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EMPLEADO')
            and   name  = 'DIV_EMP_FK'
            and   indid > 0
            and   indid < 255)
   drop index EMPLEADO.DIV_EMP_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('EMPLEADO')
            and   type = 'U')
   drop table EMPLEADO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQUIPO')
            and   name  = 'MOD_EQU_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQUIPO.MOD_EQU_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQUIPO')
            and   name  = 'MRC_EQU_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQUIPO.MRC_EQU_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQUIPO')
            and   name  = 'PROV_EQU_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQUIPO.PROV_EQU_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQUIPO')
            and   name  = 'TDE_EQU_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQUIPO.TDE_EQU_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('EQUIPO')
            and   type = 'U')
   drop table EQUIPO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQU_BOD')
            and   name  = 'SE_ALMACENA4_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQU_BOD.SE_ALMACENA4_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQU_BOD')
            and   name  = 'ALMACENA4_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQU_BOD.ALMACENA4_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('EQU_BOD')
            and   type = 'U')
   drop table EQU_BOD
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQU_PDV')
            and   name  = 'SE_ALMACENA3_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQU_PDV.SE_ALMACENA3_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('EQU_PDV')
            and   name  = 'ALMACENA3_FK'
            and   indid > 0
            and   indid < 255)
   drop index EQU_PDV.ALMACENA3_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('EQU_PDV')
            and   type = 'U')
   drop table EQU_PDV
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FACTURA')
            and   name  = 'FAC_CLI_FK'
            and   indid > 0
            and   indid < 255)
   drop index FACTURA.FAC_CLI_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FACTURA')
            and   name  = 'FAC_RES_FK'
            and   indid > 0
            and   indid < 255)
   drop index FACTURA.FAC_RES_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('FACTURA')
            and   type = 'U')
   drop table FACTURA
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FAC_EQU')
            and   name  = 'VENDE2_FK'
            and   indid > 0
            and   indid < 255)
   drop index FAC_EQU.VENDE2_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FAC_EQU')
            and   name  = 'REGISTRADO_EN_FK'
            and   indid > 0
            and   indid < 255)
   drop index FAC_EQU.REGISTRADO_EN_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('FAC_EQU')
            and   type = 'U')
   drop table FAC_EQU
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FAC_REP')
            and   name  = 'VENDE_FK'
            and   indid > 0
            and   indid < 255)
   drop index FAC_REP.VENDE_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('FAC_REP')
            and   name  = 'ES_REGISTRADO_FK'
            and   indid > 0
            and   indid < 255)
   drop index FAC_REP.ES_REGISTRADO_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('FAC_REP')
            and   type = 'U')
   drop table FAC_REP
go

if exists (select 1
            from  sysobjects
            where id = object_id('INCOTERM')
            and   type = 'U')
   drop table INCOTERM
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('LOCALIZACION')
            and   name  = 'RELATIONSHIP_35_FK'
            and   indid > 0
            and   indid < 255)
   drop index LOCALIZACION.RELATIONSHIP_35_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('LOCALIZACION')
            and   type = 'U')
   drop table LOCALIZACION
go

if exists (select 1
            from  sysobjects
            where id = object_id('LOGIN')
            and   type = 'U')
   drop table LOGIN
go

if exists (select 1
            from  sysobjects
            where id = object_id('MARCA')
            and   type = 'U')
   drop table MARCA
go

if exists (select 1
            from  sysobjects
            where id = object_id('MODELO')
            and   type = 'U')
   drop table MODELO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('OC_EQU')
            and   name  = 'CONTIENE3_FK'
            and   indid > 0
            and   indid < 255)
   drop index OC_EQU.CONTIENE3_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('OC_EQU')
            and   name  = 'ES_REGISTADO_FK'
            and   indid > 0
            and   indid < 255)
   drop index OC_EQU.ES_REGISTADO_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('OC_EQU')
            and   type = 'U')
   drop table OC_EQU
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('OC_REP')
            and   name  = 'TIENE_FK'
            and   indid > 0
            and   indid < 255)
   drop index OC_REP.TIENE_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('OC_REP')
            and   name  = 'ES_REGISTRADO2_FK'
            and   indid > 0
            and   indid < 255)
   drop index OC_REP.ES_REGISTRADO2_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('OC_REP')
            and   type = 'U')
   drop table OC_REP
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('ORDEN_C')
            and   name  = 'OC_PROV_FK'
            and   indid > 0
            and   indid < 255)
   drop index ORDEN_C.OC_PROV_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('ORDEN_C')
            and   name  = 'OC_INC_FK'
            and   indid > 0
            and   indid < 255)
   drop index ORDEN_C.OC_INC_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('ORDEN_C')
            and   type = 'U')
   drop table ORDEN_C
go

if exists (select 1
            from  sysobjects
            where id = object_id('PDV')
            and   type = 'U')
   drop table PDV
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PRECIO')
            and   name  = 'REP_PRE_FK'
            and   indid > 0
            and   indid < 255)
   drop index PRECIO.REP_PRE_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PRECIO')
            and   name  = 'EQU_PRE_FK'
            and   indid > 0
            and   indid < 255)
   drop index PRECIO.EQU_PRE_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('PRECIO')
            and   type = 'U')
   drop table PRECIO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PRESUPUESTO')
            and   name  = 'DIV_PRE_FK'
            and   indid > 0
            and   indid < 255)
   drop index PRESUPUESTO.DIV_PRE_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('PRESUPUESTO')
            and   type = 'U')
   drop table PRESUPUESTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('PROVEEDOR')
            and   type = 'U')
   drop table PROVEEDOR
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROV_REP')
            and   name  = 'SUMINISTRA_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROV_REP.SUMINISTRA_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('PROV_REP')
            and   name  = 'ES_SUMINISTRADO_FK'
            and   indid > 0
            and   indid < 255)
   drop index PROV_REP.ES_SUMINISTRADO_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('PROV_REP')
            and   type = 'U')
   drop table PROV_REP
go

if exists (select 1
            from  sysobjects
            where id = object_id('REGISTRO_ES')
            and   type = 'U')
   drop table REGISTRO_ES
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REG_EQU')
            and   name  = 'INCLUYE_FK'
            and   indid > 0
            and   indid < 255)
   drop index REG_EQU.INCLUYE_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REG_EQU')
            and   name  = 'SE_INCLUYE_FK'
            and   indid > 0
            and   indid < 255)
   drop index REG_EQU.SE_INCLUYE_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('REG_EQU')
            and   type = 'U')
   drop table REG_EQU
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REG_REP')
            and   name  = 'INCLUYE2_FK'
            and   indid > 0
            and   indid < 255)
   drop index REG_REP.INCLUYE2_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REG_REP')
            and   name  = 'SE_INCLUYE2_FK'
            and   indid > 0
            and   indid < 255)
   drop index REG_REP.SE_INCLUYE2_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('REG_REP')
            and   type = 'U')
   drop table REG_REP
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REPUESTO')
            and   name  = 'TDR_REP_FK'
            and   indid > 0
            and   indid < 255)
   drop index REPUESTO.TDR_REP_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('REPUESTO')
            and   type = 'U')
   drop table REPUESTO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REP_BOD')
            and   name  = 'SE_ALMACENA2_FK'
            and   indid > 0
            and   indid < 255)
   drop index REP_BOD.SE_ALMACENA2_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REP_BOD')
            and   name  = 'ALMACENA2_FK'
            and   indid > 0
            and   indid < 255)
   drop index REP_BOD.ALMACENA2_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('REP_BOD')
            and   type = 'U')
   drop table REP_BOD
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REP_PDV')
            and   name  = 'SE_ALMACENA_FK'
            and   indid > 0
            and   indid < 255)
   drop index REP_PDV.SE_ALMACENA_FK
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('REP_PDV')
            and   name  = 'ALMACENA_FK'
            and   indid > 0
            and   indid < 255)
   drop index REP_PDV.ALMACENA_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('REP_PDV')
            and   type = 'U')
   drop table REP_PDV
go

if exists (select 1
            from  sysobjects
            where id = object_id('RESOLUCION')
            and   type = 'U')
   drop table RESOLUCION
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('SOLICITUD_B')
            and   name  = 'SOLB_PDV_FK'
            and   indid > 0
            and   indid < 255)
   drop index SOLICITUD_B.SOLB_PDV_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('SOLICITUD_B')
            and   type = 'U')
   drop table SOLICITUD_B
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('TIPO_DE_EQUIPO')
            and   name  = 'RELATIONSHIP_34_FK'
            and   indid > 0
            and   indid < 255)
   drop index TIPO_DE_EQUIPO.RELATIONSHIP_34_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('TIPO_DE_EQUIPO')
            and   type = 'U')
   drop table TIPO_DE_EQUIPO
go

if exists (select 1
            from  sysindexes
           where  id    = object_id('TIPO_DE_REPUESTO')
            and   name  = 'RELATIONSHIP_33_FK'
            and   indid > 0
            and   indid < 255)
   drop index TIPO_DE_REPUESTO.RELATIONSHIP_33_FK
go

if exists (select 1
            from  sysobjects
            where id = object_id('TIPO_DE_REPUESTO')
            and   type = 'U')
   drop table TIPO_DE_REPUESTO
go

if exists (select 1
            from  sysobjects
            where id = object_id('TIPO_DOC_IMP')
            and   type = 'U')
   drop table TIPO_DOC_IMP
go

/*==============================================================*/
/* Table: BITACORA                                              */
/*==============================================================*/
create table BITACORA (
   BIT_ID               bigint                         identity,
   BIT_IDREG            bigint                         not null,
   BIT_TABLE            varchar(20)                    not null,
   BIT_USR              varchar(50)                    not null,
   BIT_DATE             datetime                       not null,
   BIT_OP               char(1)                        not null,
   constraint PK_BITACORA primary key nonclustered (BIT_ID)
)
go

/*==============================================================*/
/* Table: BODEGA                                                */
/*==============================================================*/
create table BODEGA (
   BOD_ID               int                            not null,
   BOD_NOM              varchar(50)                    not null,
   BOD_DIR              varchar(50)                    not null,
   BOD_TEL              varchar(10)                    not null,
   constraint PK_BODEGA primary key nonclustered (BOD_ID)
)
go

/*==============================================================*/
/* Table: CARGO                                                 */
/*==============================================================*/
create table CARGO (
   CAR_ID               int                            not null,
   CAR_NOMBRE           varchar(40)                    not null,
   CAR_DESCRIP          varchar(80)                    not null,
   constraint PK_CARGO primary key nonclustered (CAR_ID)
)
go

/*==============================================================*/
/* Table: CLIENTE                                               */
/*==============================================================*/
create table CLIENTE (
   CLI_NIT              bigint                         not null,
   CLI_DV               int                            null,
   CLI_NOMBRE           varchar(40)                    not null,
   CLI_DIR              varchar(40)                    not null,
   CLI_TEL              varchar(10)                    not null,
   CLI_CIUDAD           varchar(40)                    not null,
   CLI_PAIS             varchar(40)                    not null,
   CLI_TIPO             bit                            not null,
   constraint PK_CLIENTE primary key nonclustered (CLI_NIT)
)
go

/*==============================================================*/
/* Table: CONTACTO                                              */
/*==============================================================*/
create table CONTACTO (
   CON_ID               int                            not null,
   CLI_NIT              bigint                         not null,
   CON_NOMBRES          varchar(40)                    not null,
   CON_APELLIDOS        varchar(40)                    not null,
   CON_CARGO            varchar(40)                    not null,
   CON_MOVIL            varchar(15)                    null,
   CON_EMAIL            varchar(40)                    null,
   constraint PK_CONTACTO primary key nonclustered (CLI_NIT, CON_ID)
)
go

/*==============================================================*/
/* Index: CLI_CONT_FK                                           */
/*==============================================================*/
create index CLI_CONT_FK on CONTACTO (
CLI_NIT ASC
)
go

/*==============================================================*/
/* Table: COTIZACION                                            */
/*==============================================================*/
create table COTIZACION (
   COT_ID               bigint                         not null,
   CLI_NIT              bigint                         not null,
   COT_FECHA            date                           not null,
   COT_SUB              money                          not null,
   COT_TAX              money                          not null,
   COT_TOT              money                          not null,
   constraint PK_COTIZACION primary key nonclustered (COT_ID)
)
go

/*==============================================================*/
/* Index: COT_CLI_FK                                            */
/*==============================================================*/
create index COT_CLI_FK on COTIZACION (
CLI_NIT ASC
)
go

/*==============================================================*/
/* Table: COT_EQU                                               */
/*==============================================================*/
create table COT_EQU (
   COT_ID               bigint                         not null,
   EQU_ID               bigint                         not null,
   constraint PK_COT_EQU primary key (COT_ID, EQU_ID)
)
go

/*==============================================================*/
/* Index: ASIGNA2_FK                                            */
/*==============================================================*/
create index ASIGNA2_FK on COT_EQU (
COT_ID ASC
)
go

/*==============================================================*/
/* Index: CONTIENE2_FK                                          */
/*==============================================================*/
create index CONTIENE2_FK on COT_EQU (
EQU_ID ASC
)
go

/*==============================================================*/
/* Table: COT_REP                                               */
/*==============================================================*/
create table COT_REP (
   COT_ID               bigint                         not null,
   REP_ID               bigint                         not null,
   constraint PK_COT_REP primary key (COT_ID, REP_ID)
)
go

/*==============================================================*/
/* Index: ASIGNA_FK                                             */
/*==============================================================*/
create index ASIGNA_FK on COT_REP (
COT_ID ASC
)
go

/*==============================================================*/
/* Index: CONTIENE_FK                                           */
/*==============================================================*/
create index CONTIENE_FK on COT_REP (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: DIVISION                                              */
/*==============================================================*/
create table DIVISION (
   DIV_ID               int                            not null,
   DIV_NOMBRE           varchar(100)                   not null,
   DIV_DESCRIPCION      varchar(100)                   null,
   constraint PK_DIVISION primary key nonclustered (DIV_ID)
)
go

/*==============================================================*/
/* Table: DOC_IMP                                               */
/*==============================================================*/
create table DOC_IMP (
   DIM_UID              bigint                         not null,
   TDI_ID               int                            not null,
   OC_ID                bigint                         not null,
   DIM_NUM              varchar(100)                   not null,
   constraint PK_DOC_IMP primary key nonclustered (DIM_UID)
)
go

/*==============================================================*/
/* Index: TDOCI_DOCI_FK                                         */
/*==============================================================*/
create index TDOCI_DOCI_FK on DOC_IMP (
TDI_ID ASC
)
go

/*==============================================================*/
/* Index: OC_DOCI_FK                                            */
/*==============================================================*/
create index OC_DOCI_FK on DOC_IMP (
OC_ID ASC
)
go

/*==============================================================*/
/* Table: EMPLEADO                                              */
/*==============================================================*/
create table EMPLEADO (
   EMPL_ID              bigint                         not null,
   DIV_ID               int                            not null,
   CAR_ID               int                            not null,
   EMPL_NOMBRE          varchar(40)                    not null,
   EMPL_APELLIDO        varchar(40)                    not null,
   EMPL_DIR             varchar(40)                    null,
   EMPL_TEL             varchar(10)                    null,
   EMPL_EDAD            varchar(2)                     not null,
   constraint PK_EMPLEADO primary key nonclustered (DIV_ID, EMPL_ID)
)
go

/*==============================================================*/
/* Index: DIV_EMP_FK                                            */
/*==============================================================*/
create index DIV_EMP_FK on EMPLEADO (
DIV_ID ASC
)
go

/*==============================================================*/
/* Index: EMP_CAR_FK                                            */
/*==============================================================*/
create index EMP_CAR_FK on EMPLEADO (
CAR_ID ASC
)
go

/*==============================================================*/
/* Table: EQUIPO                                                */
/*==============================================================*/
create table EQUIPO (
   EQU_ID               bigint                         not null,
   MARC_ID              bigint                         not null,
   PROV_ID              int                            null,
   MOD_ID               bigint                         not null,
   TDE_ID               bigint                         not null,
   EQU_SN               varchar(50)                    null,
   EQU_DESCR            varchar(80)                    null,
   EQU_VOBO             bit                            not null,
   EQU_ESTADO           bit                            not null,
   constraint PK_EQUIPO primary key nonclustered (EQU_ID)
)
go

/*==============================================================*/
/* Index: TDE_EQU_FK                                            */
/*==============================================================*/
create index TDE_EQU_FK on EQUIPO (
TDE_ID ASC
)
go

/*==============================================================*/
/* Index: PROV_EQU_FK                                           */
/*==============================================================*/
create index PROV_EQU_FK on EQUIPO (
PROV_ID ASC
)
go

/*==============================================================*/
/* Index: MRC_EQU_FK                                            */
/*==============================================================*/
create index MRC_EQU_FK on EQUIPO (
MARC_ID ASC
)
go

/*==============================================================*/
/* Index: MOD_EQU_FK                                            */
/*==============================================================*/
create index MOD_EQU_FK on EQUIPO (
MOD_ID ASC
)
go

/*==============================================================*/
/* Table: EQU_BOD                                               */
/*==============================================================*/
create table EQU_BOD (
   EQU_ID               bigint                         not null,
   BOD_ID               int                            not null,
   constraint PK_EQU_BOD primary key (EQU_ID, BOD_ID)
)
go

/*==============================================================*/
/* Index: ALMACENA4_FK                                          */
/*==============================================================*/
create index ALMACENA4_FK on EQU_BOD (
EQU_ID ASC
)
go

/*==============================================================*/
/* Index: SE_ALMACENA4_FK                                       */
/*==============================================================*/
create index SE_ALMACENA4_FK on EQU_BOD (
BOD_ID ASC
)
go

/*==============================================================*/
/* Table: EQU_PDV                                               */
/*==============================================================*/
create table EQU_PDV (
   EQU_ID               bigint                         not null,
   PDV_ID               int                            not null,
   constraint PK_EQU_PDV primary key (EQU_ID, PDV_ID)
)
go

/*==============================================================*/
/* Index: ALMACENA3_FK                                          */
/*==============================================================*/
create index ALMACENA3_FK on EQU_PDV (
EQU_ID ASC
)
go

/*==============================================================*/
/* Index: SE_ALMACENA3_FK                                       */
/*==============================================================*/
create index SE_ALMACENA3_FK on EQU_PDV (
PDV_ID ASC
)
go

/*==============================================================*/
/* Table: FACTURA                                               */
/*==============================================================*/
create table FACTURA (
   FAC_ID               bigint                         not null,
   CLI_NIT              bigint                         not null,
   RES_ID               int                            not null,
   FAC_FE               date                           not null,
   FAC_FR               date                           not null,
   FAC_FV               date                           not null,
   FAC_SUB              money                          not null,
   FAC_TAX              money                          not null,
   FAC_TOTAL            money                          not null,
   FAC_EST              varchar(1)                     not null,
   constraint PK_FACTURA primary key nonclustered (FAC_ID)
)
go

/*==============================================================*/
/* Index: FAC_RES_FK                                            */
/*==============================================================*/
create index FAC_RES_FK on FACTURA (
RES_ID ASC
)
go

/*==============================================================*/
/* Index: FAC_CLI_FK                                            */
/*==============================================================*/
create index FAC_CLI_FK on FACTURA (
CLI_NIT ASC
)
go

/*==============================================================*/
/* Table: FAC_EQU                                               */
/*==============================================================*/
create table FAC_EQU (
   FAC_ID               bigint                         not null,
   EQU_ID               bigint                         not null,
   constraint PK_FAC_EQU primary key (FAC_ID, EQU_ID)
)
go

/*==============================================================*/
/* Index: REGISTRADO_EN_FK                                      */
/*==============================================================*/
create index REGISTRADO_EN_FK on FAC_EQU (
FAC_ID ASC
)
go

/*==============================================================*/
/* Index: VENDE2_FK                                             */
/*==============================================================*/
create index VENDE2_FK on FAC_EQU (
EQU_ID ASC
)
go

/*==============================================================*/
/* Table: FAC_REP                                               */
/*==============================================================*/
create table FAC_REP (
   FAC_ID               bigint                         not null,
   REP_ID               bigint                         not null,
   constraint PK_FAC_REP primary key (FAC_ID, REP_ID)
)
go

/*==============================================================*/
/* Index: ES_REGISTRADO_FK                                      */
/*==============================================================*/
create index ES_REGISTRADO_FK on FAC_REP (
FAC_ID ASC
)
go

/*==============================================================*/
/* Index: VENDE_FK                                              */
/*==============================================================*/
create index VENDE_FK on FAC_REP (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: INCOTERM                                              */
/*==============================================================*/
create table INCOTERM (
   INC_ID               bigint                         not null,
   INC_NOM              char(3)                        not null,
   INC_DESCRIP          varchar(120)                   not null,
   constraint PK_INCOTERM primary key nonclustered (INC_ID)
)
go

/*==============================================================*/
/* Table: LOCALIZACION                                          */
/*==============================================================*/
create table LOCALIZACION (
   LOC_ID               bigint                         not null,
   LOC_LOC_ID           bigint                         null,
   LOC_DESC             varchar(100)                   not null,
   LOC_TIPO             varchar(100)                   not null,
   constraint PK_LOCALIZACION primary key nonclustered (LOC_ID)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_35_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_35_FK on LOCALIZACION (
LOC_LOC_ID ASC
)
go

/*==============================================================*/
/* Table: LOGIN                                                 */
/*==============================================================*/
create table LOGIN (
   LOGIN_ID             int                            not null,
   LOGIN_USR            varchar(30)                    not null,
   LOGIN_PASS           varchar(300)                   not null,
   constraint PK_LOGIN primary key nonclustered (LOGIN_ID)
)
go

/*==============================================================*/
/* Table: MARCA                                                 */
/*==============================================================*/
create table MARCA (
   MARC_ID              bigint                         not null,
   MARC                 varchar(100)                   not null,
   constraint PK_MARCA primary key nonclustered (MARC_ID)
)
go

/*==============================================================*/
/* Table: MODELO                                                */
/*==============================================================*/
create table MODELO (
   MOD_ID               bigint                         not null,
   MOD                  varchar(100)                   not null,
   constraint PK_MODELO primary key nonclustered (MOD_ID)
)
go

/*==============================================================*/
/* Table: OC_EQU                                                */
/*==============================================================*/
create table OC_EQU (
   OC_ID                bigint                         not null,
   EQU_ID               bigint                         not null,
   constraint PK_OC_EQU primary key (OC_ID, EQU_ID)
)
go

/*==============================================================*/
/* Index: ES_REGISTADO_FK                                       */
/*==============================================================*/
create index ES_REGISTADO_FK on OC_EQU (
OC_ID ASC
)
go

/*==============================================================*/
/* Index: CONTIENE3_FK                                          */
/*==============================================================*/
create index CONTIENE3_FK on OC_EQU (
EQU_ID ASC
)
go

/*==============================================================*/
/* Table: OC_REP                                                */
/*==============================================================*/
create table OC_REP (
   OC_ID                bigint                         not null,
   REP_ID               bigint                         not null,
   constraint PK_OC_REP primary key (OC_ID, REP_ID)
)
go

/*==============================================================*/
/* Index: ES_REGISTRADO2_FK                                     */
/*==============================================================*/
create index ES_REGISTRADO2_FK on OC_REP (
OC_ID ASC
)
go

/*==============================================================*/
/* Index: TIENE_FK                                              */
/*==============================================================*/
create index TIENE_FK on OC_REP (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: ORDEN_C                                               */
/*==============================================================*/
create table ORDEN_C (
   OC_ID                bigint                         not null,
   INC_ID               bigint                         not null,
   PROV_ID              int                            not null,
   OC_FECHA_EM          datetime                       not null,
   OC_FECHA_ENT         datetime                       not null,
   OC_VBASE             money                          not null,
   OC_VTAX              money                          not null,
   OC_VFLETES           money                          not null,
   OC_VSEGUROS          money                          not null,
   OC_VNAC              money                          not null,
   OC_ESTADO            char(1)                        not null,
   constraint PK_ORDEN_C primary key nonclustered (OC_ID)
)
go

/*==============================================================*/
/* Index: OC_INC_FK                                             */
/*==============================================================*/
create index OC_INC_FK on ORDEN_C (
INC_ID ASC
)
go

/*==============================================================*/
/* Index: OC_PROV_FK                                            */
/*==============================================================*/
create index OC_PROV_FK on ORDEN_C (
PROV_ID ASC
)
go

/*==============================================================*/
/* Table: PDV                                                   */
/*==============================================================*/
create table PDV (
   PDV_ID               int                            not null,
   PDV_NOMBRE           varchar(40)                    not null,
   PDV_DIR              varchar(40)                    not null,
   PDV_TEL              varchar(10)                    null,
   constraint PK_PDV primary key nonclustered (PDV_ID)
)
go

/*==============================================================*/
/* Table: PRECIO                                                */
/*==============================================================*/
create table PRECIO (
   PRE_ID               bigint                         not null,
   REP_ID               bigint                         null,
   EQU_ID               bigint                         null,
   PRE_VAL              money                          not null,
   PRE_FI               date                           not null,
   PRE_FF               date                           not null,
   PRE_ESTADO           bit                            not null,
   constraint PK_PRECIO primary key nonclustered (PRE_ID)
)
go

/*==============================================================*/
/* Index: EQU_PRE_FK                                            */
/*==============================================================*/
create index EQU_PRE_FK on PRECIO (
EQU_ID ASC
)
go

/*==============================================================*/
/* Index: REP_PRE_FK                                            */
/*==============================================================*/
create index REP_PRE_FK on PRECIO (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: PRESUPUESTO                                           */
/*==============================================================*/
create table PRESUPUESTO (
   PRES_ID              int                            not null,
   DIV_ID               int                            not null,
   PRES_VALOR           money                          not null,
   PRES_FECHAI          date                           not null,
   PRES_FECHAF          date                           not null,
   constraint PK_PRESUPUESTO primary key nonclustered (PRES_ID)
)
go

/*==============================================================*/
/* Index: DIV_PRE_FK                                            */
/*==============================================================*/
create index DIV_PRE_FK on PRESUPUESTO (
DIV_ID ASC
)
go

/*==============================================================*/
/* Table: PROVEEDOR                                             */
/*==============================================================*/
create table PROVEEDOR (
   PROV_ID              int                            not null,
   PROV_NOM             varchar(50)                    not null,
   PROV_DIR             varchar(50)                    not null,
   PROV_CIUDAD          varchar(40)                    not null,
   PROV_PAIS            varchar(40)                    not null,
   PROV_IBAN            varchar(24)                    null,
   PROV_SWIFT           varchar(11)                    null,
   PROV_ESTADO          bit                            not null,
   constraint PK_PROVEEDOR primary key nonclustered (PROV_ID)
)
go

/*==============================================================*/
/* Table: PROV_REP                                              */
/*==============================================================*/
create table PROV_REP (
   PROV_ID              int                            not null,
   REP_ID               bigint                         not null,
   constraint PK_PROV_REP primary key (PROV_ID, REP_ID)
)
go

/*==============================================================*/
/* Index: ES_SUMINISTRADO_FK                                    */
/*==============================================================*/
create index ES_SUMINISTRADO_FK on PROV_REP (
PROV_ID ASC
)
go

/*==============================================================*/
/* Index: SUMINISTRA_FK                                         */
/*==============================================================*/
create index SUMINISTRA_FK on PROV_REP (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: REGISTRO_ES                                           */
/*==============================================================*/
create table REGISTRO_ES (
   REG_ID               bigint                         not null,
   REG_FECHA            datetime                       not null,
   REG_TIPO             bit                            not null,
   REG_ESTADO           bit                            not null,
   constraint PK_REGISTRO_ES primary key nonclustered (REG_ID)
)
go

/*==============================================================*/
/* Table: REG_EQU                                               */
/*==============================================================*/
create table REG_EQU (
   REG_ID               bigint                         not null,
   EQU_ID               bigint                         not null,
   constraint PK_REG_EQU primary key (REG_ID, EQU_ID)
)
go

/*==============================================================*/
/* Index: SE_INCLUYE_FK                                         */
/*==============================================================*/
create index SE_INCLUYE_FK on REG_EQU (
REG_ID ASC
)
go

/*==============================================================*/
/* Index: INCLUYE_FK                                            */
/*==============================================================*/
create index INCLUYE_FK on REG_EQU (
EQU_ID ASC
)
go

/*==============================================================*/
/* Table: REG_REP                                               */
/*==============================================================*/
create table REG_REP (
   REG_ID               bigint                         not null,
   REP_ID               bigint                         not null,
   constraint PK_REG_REP primary key (REG_ID, REP_ID)
)
go

/*==============================================================*/
/* Index: SE_INCLUYE2_FK                                        */
/*==============================================================*/
create index SE_INCLUYE2_FK on REG_REP (
REG_ID ASC
)
go

/*==============================================================*/
/* Index: INCLUYE2_FK                                           */
/*==============================================================*/
create index INCLUYE2_FK on REG_REP (
REP_ID ASC
)
go

/*==============================================================*/
/* Table: REPUESTO                                              */
/*==============================================================*/
create table REPUESTO (
   REP_ID               bigint                         not null,
   TDR_ID               bigint                         not null,
   REP_NPARTE           varchar(50)                    not null,
   REP_SN               varchar(50)                    null,
   REP_DESCRIP          varchar(80)                    null,
   REP_VOBO             bit                            not null,
   REP_ESTADO           bit                            not null,
   constraint PK_REPUESTO primary key nonclustered (REP_ID)
)
go

/*==============================================================*/
/* Index: TDR_REP_FK                                            */
/*==============================================================*/
create index TDR_REP_FK on REPUESTO (
TDR_ID ASC
)
go

/*==============================================================*/
/* Table: REP_BOD                                               */
/*==============================================================*/
create table REP_BOD (
   REP_ID               bigint                         not null,
   BOD_ID               int                            not null,
   constraint PK_REP_BOD primary key (REP_ID, BOD_ID)
)
go

/*==============================================================*/
/* Index: ALMACENA2_FK                                          */
/*==============================================================*/
create index ALMACENA2_FK on REP_BOD (
REP_ID ASC
)
go

/*==============================================================*/
/* Index: SE_ALMACENA2_FK                                       */
/*==============================================================*/
create index SE_ALMACENA2_FK on REP_BOD (
BOD_ID ASC
)
go

/*==============================================================*/
/* Table: REP_PDV                                               */
/*==============================================================*/
create table REP_PDV (
   REP_ID               bigint                         not null,
   PDV_ID               int                            not null,
   constraint PK_REP_PDV primary key (REP_ID, PDV_ID)
)
go

/*==============================================================*/
/* Index: ALMACENA_FK                                           */
/*==============================================================*/
create index ALMACENA_FK on REP_PDV (
REP_ID ASC
)
go

/*==============================================================*/
/* Index: SE_ALMACENA_FK                                        */
/*==============================================================*/
create index SE_ALMACENA_FK on REP_PDV (
PDV_ID ASC
)
go

/*==============================================================*/
/* Table: RESOLUCION                                            */
/*==============================================================*/
create table RESOLUCION (
   RES_ID               int                            not null,
   RES_LIMINF           bigint                         not null,
   RES_LIMSUP           bigint                         not null,
   RES_FEM              date                           not null,
   constraint PK_RESOLUCION primary key nonclustered (RES_ID)
)
go

/*==============================================================*/
/* Table: SOLICITUD_B                                           */
/*==============================================================*/
create table SOLICITUD_B (
   SOLB_ID              int                            not null,
   PDV_ID               int                            not null,
   SOL_FECHA            date                           not null,
   SOL_ESTADO           bit                            not null,
   constraint PK_SOLICITUD_B primary key nonclustered (SOLB_ID)
)
go

/*==============================================================*/
/* Index: SOLB_PDV_FK                                           */
/*==============================================================*/
create index SOLB_PDV_FK on SOLICITUD_B (
PDV_ID ASC
)
go

/*==============================================================*/
/* Table: TIPO_DE_EQUIPO                                        */
/*==============================================================*/
create table TIPO_DE_EQUIPO (
   TDE_ID               bigint                         not null,
   TIP_TDE_ID           bigint                         null,
   TDE_TIPO             varchar(50)                    not null,
   TDE_DESCRIP          varchar(80)                    null,
   constraint PK_TIPO_DE_EQUIPO primary key nonclustered (TDE_ID)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_34_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_34_FK on TIPO_DE_EQUIPO (
TIP_TDE_ID ASC
)
go

/*==============================================================*/
/* Table: TIPO_DE_REPUESTO                                      */
/*==============================================================*/
create table TIPO_DE_REPUESTO (
   TDR_ID               bigint                         not null,
   TIP_TDR_ID           bigint                         null,
   TDR_NOMBRE           varchar(60)                    not null,
   TDR_DESCRIP          varchar(80)                    null,
   constraint PK_TIPO_DE_REPUESTO primary key nonclustered (TDR_ID)
)
go

/*==============================================================*/
/* Index: RELATIONSHIP_33_FK                                    */
/*==============================================================*/
create index RELATIONSHIP_33_FK on TIPO_DE_REPUESTO (
TIP_TDR_ID ASC
)
go

/*==============================================================*/
/* Table: TIPO_DOC_IMP                                          */
/*==============================================================*/
create table TIPO_DOC_IMP (
   TDI_ID               int                            not null,
   TDI_TIPO             varchar(100)                   not null,
   constraint PK_TIPO_DOC_IMP primary key nonclustered (TDI_ID)
)
go

/*==============================================================*/
/* View: V_BITACORA                                             */
/*==============================================================*/
create view V_BITACORA as
select
   BITACORA.BIT_ID,
   BITACORA.BIT_IDREG,
   BITACORA.BIT_TABLE,
   BITACORA.BIT_USR,
   BITACORA.BIT_DATE,
   BITACORA.BIT_OP
from
   BITACORA
go

/*==============================================================*/
/* View: V_BODEGA                                               */
/*==============================================================*/
create view V_BODEGA as
select
   BODEGA.BOD_ID,
   BODEGA.BOD_NOM,
   BODEGA.BOD_DIR,
   BODEGA.BOD_TEL
from
   BODEGA
go

/*==============================================================*/
/* View: V_CARGO                                                */
/*==============================================================*/
create view V_CARGO as
select
   CARGO.CAR_ID,
   CARGO.CAR_NOMBRE,
   CARGO.CAR_DESCRIP
from
   CARGO
go

/*==============================================================*/
/* View: V_CLIENTE                                              */
/*==============================================================*/
create view V_CLIENTE as
select
   CLIENTE.CLI_NIT,
   CLIENTE.CLI_DV,
   CLIENTE.CLI_NOMBRE,
   CLIENTE.CLI_DIR,
   CLIENTE.CLI_TEL,
   CLIENTE.CLI_CIUDAD,
   CLIENTE.CLI_PAIS,
   CLIENTE.CLI_TIPO
from
   CLIENTE
go

/*==============================================================*/
/* View: V_CONTACTO                                             */
/*==============================================================*/
create view V_CONTACTO as
select
   CONTACTO.CON_ID,
   CONTACTO.CLI_NIT,
   CONTACTO.CON_NOMBRES,
   CONTACTO.CON_APELLIDOS,
   CONTACTO.CON_CARGO,
   CONTACTO.CON_MOVIL,
   CONTACTO.CON_EMAIL
from
   CONTACTO
go

/*==============================================================*/
/* View: V_COTIZACION                                           */
/*==============================================================*/
create view V_COTIZACION as
select
   COTIZACION.COT_ID,
   COTIZACION.CLI_NIT,
   COTIZACION.COT_FECHA,
   COTIZACION.COT_SUB,
   COTIZACION.COT_TAX,
   COTIZACION.COT_TOT
from
   COTIZACION
go

/*==============================================================*/
/* View: V_COT_EQU                                              */
/*==============================================================*/
create view V_COT_EQU as
select
   COT_EQU.COT_ID,
   COT_EQU.EQU_ID
from
   COT_EQU
go

/*==============================================================*/
/* View: V_COT_REP                                              */
/*==============================================================*/
create view V_COT_REP as
select
   COT_REP.COT_ID,
   COT_REP.REP_ID
from
   COT_REP
go

/*==============================================================*/
/* View: V_DIVISION                                             */
/*==============================================================*/
create view V_DIVISION as
select
   DIVISION.DIV_ID,
   DIVISION.DIV_NOMBRE,
   DIVISION.DIV_DESCRIPCION
from
   DIVISION
go

/*==============================================================*/
/* View: V_DOC_IMP                                              */
/*==============================================================*/
create view V_DOC_IMP as
select
   DOC_IMP.DIM_UID,
   DOC_IMP.TDI_ID,
   DOC_IMP.OC_ID,
   DOC_IMP.DIM_NUM
from
   DOC_IMP
go

/*==============================================================*/
/* View: V_EMPLEADO                                             */
/*==============================================================*/
create view V_EMPLEADO as
select
   EMPLEADO.EMPL_ID,
   EMPLEADO.DIV_ID,
   EMPLEADO.CAR_ID,
   EMPLEADO.EMPL_NOMBRE,
   EMPLEADO.EMPL_APELLIDO,
   EMPLEADO.EMPL_DIR,
   EMPLEADO.EMPL_TEL,
   EMPLEADO.EMPL_EDAD
from
   EMPLEADO
go

/*==============================================================*/
/* View: V_EQUIPO                                               */
/*==============================================================*/
create view V_EQUIPO as
select
   EQUIPO.EQU_ID,
   EQUIPO.MARC_ID,
   EQUIPO.PROV_ID,
   EQUIPO.MOD_ID,
   EQUIPO.TDE_ID,
   EQUIPO.EQU_SN,
   EQUIPO.EQU_DESCR,
   EQUIPO.EQU_VOBO,
   EQUIPO.EQU_ESTADO
from
   EQUIPO
go

/*==============================================================*/
/* View: V_EQU_BOD                                              */
/*==============================================================*/
create view V_EQU_BOD as
select
   EQU_BOD.EQU_ID,
   EQU_BOD.BOD_ID
from
   EQU_BOD
go

/*==============================================================*/
/* View: V_EQU_PDV                                              */
/*==============================================================*/
create view V_EQU_PDV as
select
   EQU_PDV.EQU_ID,
   EQU_PDV.PDV_ID
from
   EQU_PDV
go

/*==============================================================*/
/* View: V_FACTURA                                              */
/*==============================================================*/
create view V_FACTURA as
select
   FACTURA.FAC_ID,
   FACTURA.CLI_NIT,
   FACTURA.RES_ID,
   FACTURA.FAC_FE,
   FACTURA.FAC_FR,
   FACTURA.FAC_FV,
   FACTURA.FAC_SUB,
   FACTURA.FAC_TAX,
   FACTURA.FAC_TOTAL,
   FACTURA.FAC_EST
from
   FACTURA
go

/*==============================================================*/
/* View: V_FAC_EQU                                              */
/*==============================================================*/
create view V_FAC_EQU as
select
   FAC_EQU.FAC_ID,
   FAC_EQU.EQU_ID
from
   FAC_EQU
go

/*==============================================================*/
/* View: V_FAC_REP                                              */
/*==============================================================*/
create view V_FAC_REP as
select
   FAC_REP.FAC_ID,
   FAC_REP.REP_ID
from
   FAC_REP
go

/*==============================================================*/
/* View: V_INCOTERM                                             */
/*==============================================================*/
create view V_INCOTERM as
select
   INCOTERM.INC_ID,
   INCOTERM.INC_NOM,
   INCOTERM.INC_DESCRIP
from
   INCOTERM
go

/*==============================================================*/
/* View: V_LOCALIZACION                                         */
/*==============================================================*/
create view V_LOCALIZACION as
select
   LOCALIZACION.LOC_ID,
   LOCALIZACION.LOC_LOC_ID,
   LOCALIZACION.LOC_DESC,
   LOCALIZACION.LOC_TIPO
from
   LOCALIZACION
go

/*==============================================================*/
/* View: V_LOGIN                                                */
/*==============================================================*/
create view V_LOGIN as
select
   LOGIN.LOGIN_ID,
   LOGIN.LOGIN_USR,
   LOGIN.LOGIN_PASS
from
   LOGIN
go

/*==============================================================*/
/* View: V_MARCA                                                */
/*==============================================================*/
create view V_MARCA as
select
   MARCA.MARC_ID,
   MARCA.MARC
from
   MARCA
go

/*==============================================================*/
/* View: V_MODELO                                               */
/*==============================================================*/
create view V_MODELO as
select
   MODELO.MOD_ID,
   MODELO.MOD
from
   MODELO
go

/*==============================================================*/
/* View: V_OC_EQU                                               */
/*==============================================================*/
create view V_OC_EQU as
select
   OC_EQU.OC_ID,
   OC_EQU.EQU_ID
from
   OC_EQU
go

/*==============================================================*/
/* View: V_OC_REP                                               */
/*==============================================================*/
create view V_OC_REP as
select
   OC_REP.OC_ID,
   OC_REP.REP_ID
from
   OC_REP
go

/*==============================================================*/
/* View: V_ORDEN_C                                              */
/*==============================================================*/
create view V_ORDEN_C as
select
   ORDEN_C.OC_ID,
   ORDEN_C.INC_ID,
   ORDEN_C.PROV_ID,
   ORDEN_C.OC_FECHA_EM,
   ORDEN_C.OC_FECHA_ENT,
   ORDEN_C.OC_VBASE,
   ORDEN_C.OC_VTAX,
   ORDEN_C.OC_VFLETES,
   ORDEN_C.OC_VSEGUROS,
   ORDEN_C.OC_VNAC,
   ORDEN_C.OC_ESTADO
from
   ORDEN_C
go

/*==============================================================*/
/* View: V_PDV                                                  */
/*==============================================================*/
create view V_PDV as
select
   PDV.PDV_ID,
   PDV.PDV_NOMBRE,
   PDV.PDV_DIR,
   PDV.PDV_TEL
from
   PDV
go

/*==============================================================*/
/* View: V_PRECIO                                               */
/*==============================================================*/
create view V_PRECIO as
select
   PRECIO.PRE_ID,
   PRECIO.REP_ID,
   PRECIO.EQU_ID,
   PRECIO.PRE_VAL,
   PRECIO.PRE_FI,
   PRECIO.PRE_FF,
   PRECIO.PRE_ESTADO
from
   PRECIO
go

/*==============================================================*/
/* View: V_PRESUPUESTO                                          */
/*==============================================================*/
create view V_PRESUPUESTO as
select
   PRESUPUESTO.PRES_ID,
   PRESUPUESTO.DIV_ID,
   PRESUPUESTO.PRES_VALOR,
   PRESUPUESTO.PRES_FECHAI,
   PRESUPUESTO.PRES_FECHAF
from
   PRESUPUESTO
go

/*==============================================================*/
/* View: V_PROVEEDOR                                            */
/*==============================================================*/
create view V_PROVEEDOR as
select
   PROVEEDOR.PROV_ID,
   PROVEEDOR.PROV_NOM,
   PROVEEDOR.PROV_DIR,
   PROVEEDOR.PROV_CIUDAD,
   PROVEEDOR.PROV_PAIS,
   PROVEEDOR.PROV_IBAN,
   PROVEEDOR.PROV_SWIFT,
   PROVEEDOR.PROV_ESTADO
from
   PROVEEDOR
go

/*==============================================================*/
/* View: V_PROV_REP                                             */
/*==============================================================*/
create view V_PROV_REP as
select
   PROV_REP.PROV_ID,
   PROV_REP.REP_ID
from
   PROV_REP
go

/*==============================================================*/
/* View: V_REGISTRO_ES                                          */
/*==============================================================*/
create view V_REGISTRO_ES as
select
   REGISTRO_ES.REG_ID,
   REGISTRO_ES.REG_FECHA,
   REGISTRO_ES.REG_TIPO,
   REGISTRO_ES.REG_ESTADO
from
   REGISTRO_ES
go

/*==============================================================*/
/* View: V_REG_EQU                                              */
/*==============================================================*/
create view V_REG_EQU as
select
   REG_EQU.REG_ID,
   REG_EQU.EQU_ID
from
   REG_EQU
go

/*==============================================================*/
/* View: V_REG_REP                                              */
/*==============================================================*/
create view V_REG_REP as
select
   REG_REP.REG_ID,
   REG_REP.REP_ID
from
   REG_REP
go

/*==============================================================*/
/* View: V_REPUESTO                                             */
/*==============================================================*/
create view V_REPUESTO as
select
   REPUESTO.REP_ID,
   REPUESTO.TDR_ID,
   REPUESTO.REP_NPARTE,
   REPUESTO.REP_SN,
   REPUESTO.REP_DESCRIP,
   REPUESTO.REP_VOBO,
   REPUESTO.REP_ESTADO
from
   REPUESTO
go

/*==============================================================*/
/* View: V_REP_BOD                                              */
/*==============================================================*/
create view V_REP_BOD as
select
   REP_BOD.REP_ID,
   REP_BOD.BOD_ID
from
   REP_BOD
go

/*==============================================================*/
/* View: V_REP_PDV                                              */
/*==============================================================*/
create view V_REP_PDV as
select
   REP_PDV.REP_ID,
   REP_PDV.PDV_ID
from
   REP_PDV
go

/*==============================================================*/
/* View: V_RESOLUCION                                           */
/*==============================================================*/
create view V_RESOLUCION as
select
   RESOLUCION.RES_ID,
   RESOLUCION.RES_LIMINF,
   RESOLUCION.RES_LIMSUP,
   RESOLUCION.RES_FEM
from
   RESOLUCION
go

/*==============================================================*/
/* View: V_SOLICITUD_B                                          */
/*==============================================================*/
create view V_SOLICITUD_B as
select
   SOLICITUD_B.SOLB_ID,
   SOLICITUD_B.PDV_ID,
   SOLICITUD_B.SOL_FECHA,
   SOLICITUD_B.SOL_ESTADO
from
   SOLICITUD_B
go

/*==============================================================*/
/* View: V_TIPO_DE_EQUIPO                                       */
/*==============================================================*/
create view V_TIPO_DE_EQUIPO as
select
   TIPO_DE_EQUIPO.TDE_ID,
   TIPO_DE_EQUIPO.TIP_TDE_ID,
   TIPO_DE_EQUIPO.TDE_TIPO,
   TIPO_DE_EQUIPO.TDE_DESCRIP
from
   TIPO_DE_EQUIPO
go

/*==============================================================*/
/* View: V_TIPO_DE_REPUESTO                                     */
/*==============================================================*/
create view V_TIPO_DE_REPUESTO as
select
   TIPO_DE_REPUESTO.TDR_ID,
   TIPO_DE_REPUESTO.TIP_TDR_ID,
   TIPO_DE_REPUESTO.TDR_NOMBRE,
   TIPO_DE_REPUESTO.TDR_DESCRIP
from
   TIPO_DE_REPUESTO
go

/*==============================================================*/
/* View: V_TIPO_DOC_IMP                                         */
/*==============================================================*/
create view V_TIPO_DOC_IMP as
select
   TIPO_DOC_IMP.TDI_ID,
   TIPO_DOC_IMP.TDI_TIPO
from
   TIPO_DOC_IMP
go


create trigger TR_BODEGA on BODEGA
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'BODEGA'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select BOD_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT BOD_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT BOD_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_CARGO on CARGO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'CARGO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select CAR_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CAR_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CAR_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_CLIENTE on CLIENTE
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'CLIENTE'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select CLI_NIT from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CLI_NIT
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CLI_NIT
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_CONTACTO on CONTACTO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'CONTACTO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select CON_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CON_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT CON_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_COTIZACION on COTIZACION
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'COTIZACION'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select COT_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT COT_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT COT_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_DIVISION on DIVISION
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'DIVISION'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select DIV_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT DIV_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT DIV_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_DOC_IMP on DOC_IMP
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'DOC_IMP'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select DIM_UID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT DIM_UID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT DIM_UID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_EMPLEADO on EMPLEADO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'EMPLEADO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select EMPL_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT EMPL_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT EMPL_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_EQUIPO on EQUIPO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'EQUIPO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select EQU_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT EQU_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT EQU_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_FACTURA on FACTURA
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'FACTURA'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select FAC_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT FAC_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT FAC_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_INCOTERM on INCOTERM
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'INCOTERM'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select INC_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT INC_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT INC_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_LOCALIZACION on LOCALIZACION
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'LOCALIZACION'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select LOC_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT LOC_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT LOC_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_LOG_IN on LOGIN
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'LOGIN'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select LOGIN_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT LOGIN_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT LOGIN_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_MARCA on MARCA
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'MARCA'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select MARC_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT MARC_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT MARC_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_MODELO on MODELO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'MODELO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select MOD_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT MOD_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT MOD_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_ORDEN_C on ORDEN_C
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'ORDEN_C'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select OC_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT OC_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT OC_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_PDV on PDV for delete as
begin
    declare
       @numrows  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return


    return

/*  Errors handling  */
error:
    raiserror @errno @errmsg
    rollback  transaction
end
go


create trigger TR_PRECIO on PRECIO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'PRECIO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select PRE_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PRE_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PRE_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_PRESUPUESTO on PRESUPUESTO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'PRESUPUESTO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select PRES_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PRES_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PRES_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_PROVEEDOR on PROVEEDOR
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'PROVEEDOR'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select PROV_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PROV_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT PROV_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_REGISTRO_ES on REGISTRO_ES
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'REGISTRO_ES'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select REG_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT REG_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT REG_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_REPUESTO on REPUESTO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'REPUESTO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select REP_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT REP_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT REP_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_RESOLUCION on RESOLUCION
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'RESOLUCION'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select RES_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT RES_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT RES_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_SOLICITUD_B on SOLICITUD_B
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'SOLICITUD_B'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select SOLB_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT SOLB_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT SOLB_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_TIPO_DE_EQUIPO on TIPO_DE_EQUIPO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'TIPO_DE_EQUIPO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select TDE_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDE_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDE_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_TIPO_DE_REPUESTO on TIPO_DE_REPUESTO
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'TIPO_DE_REPUESTO'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select TDR_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDR_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDR_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go


create trigger TR_TIPO_DOC_IMP on TIPO_DOC_IMP
for insert, update, delete
as
	--Determine if this is an INSERT,UPDATE, or DELETE Action or a "failed delete".
    DECLARE @Action char(1)
    DECLARE @this_table varchar(20)
    SELECT @this_table = 'TIPO_DOC_IMP'
    
    SET @Action = (CASE WHEN EXISTS(SELECT * FROM inserted)
                         AND EXISTS(SELECT * FROM deleted)
                            THEN 'U'  -- Set Action to Updated.
                        WHEN EXISTS(SELECT * FROM inserted)
                            THEN 'I'  -- Set Action to Insert.
                        WHEN EXISTS(SELECT * FROM deleted)
                            THEN 'D'  -- Set Action to Deleted.
                        ELSE NULL -- Skip. It may have been a "failed delete".   
                    END)
                    
     IF (@Action = 'I')
     BEGIN
        declare @varid bigint
	    select @varid = (select TDI_ID from inserted)
        insert into BITACORA values(@varid,@this_table,USER_NAME(),GETDATE(),@Action)
     END
     
     IF (@Action = 'U')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDI_ID
        FROM inserted

    	DECLARE @varid1 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid1
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid1,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid1
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END
     
     
     IF (@Action = 'D')
     BEGIN
        DECLARE c CURSOR  FOR
        SELECT TDI_ID
        FROM deleted

    	DECLARE @varid2 bigint

    	-- Open the cursor
    	OPEN c

    	FETCH NEXT FROM c INTO @varid2
    	WHILE (@@FETCH_STATUS = 0)
    	BEGIN
    		 insert into BITACORA values(@varid2,@this_table,USER_NAME(),GETDATE(),@Action)

    		-- do other stuff

    		FETCH NEXT FROM c INTO @varid2
    	END

    	-- Close and deallocate the cursor
    	CLOSE c
    	DEALLOCATE c
     END

go

